//
//  ImageScrollerView.m
//  PianoAndPoemDirectSeeding
//
//  Created by 梅津山 on 2017/7/19.
//  Copyright © 2017年 梅津山. All rights reserved.
//

#import "ImageScrollerView.h"
#import  <UIImageView+AFNetworking.h>

#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height
@implementation ImageScrollerView

+ (instancetype)sharedRect:(CGRect)rect{
    static ImageScrollerView *imageV;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        imageV = [[ImageScrollerView alloc] initWithFrame:rect];
        //关闭弹簧效果
        imageV.bounces = NO;
        imageV.showsHorizontalScrollIndicator = NO;
        imageV.showsVerticalScrollIndicator = NO;
        //设置分页效果
        imageV.pagingEnabled = YES;
    });
    return imageV;
}

//点击图片后的方法(即图片的放大全屏效果)
- (void) tapActionImage:(NSArray *)imageArr{
    //创建一个黑色背景
    [self setBackgroundColor:[UIColor blackColor]];
    //创建显示图像的视图
    //初始化要显示的图片内容的imageView
    for (NSInteger i = 0; i < imageArr.count; i ++) {
        //创建图片视图
        UIImageView *imageV = [[UIImageView alloc] init];
        imageV.frame = CGRectMake(SCREEN_W * i, 100, SCREEN_W, 400);
        [imageV setImageWithURL:[NSURL URLWithString:imageArr[i]]];
        //添加点击手势（即点击图片后退出全屏）
        imageV.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeView)];
        [imageV addGestureRecognizer:tapGesture];
        [self addSubview:imageV];
    }
    //告诉滚动视图内容的大小
    self.contentSize = CGSizeMake(SCREEN_W * imageArr.count, 300);
    
    //放大过程中出现的缓慢动画
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.2;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    [self.layer addAnimation:animation forKey:nil];
}

//图片点击事件
-(void)closeView{
    [self removeFromSuperview];
    self.mainBlock();
}



@end
